package com.zztl.infiniteBand.presenter

import android.content.Context
import android.os.Environment

import com.zztl.infiniteBand.base.BasePresenter
import com.zztl.infiniteBand.model.BaseResponseBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.imp.RequestKeys
import com.zztl.infiniteBand.ui.view.mvpview.UploadImageView

import java.io.File

/**
 * @desc
 * @auth ${user}
 * @time 2018/6/5  14:10
 */
class UploadImagePresenter : BasePresenter<UploadImageView>() {
    fun loadImage(context: Context) {
        val mHeadPortraitFileDir = File(Environment.getExternalStorageDirectory().absolutePath + "/gvc/Image")
        if (!mHeadPortraitFileDir.exists()) {
            mHeadPortraitFileDir.mkdirs()
        }
        val file = File(mHeadPortraitFileDir.absolutePath, "/invite.png")

        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                        .uploadImage(RetorfitServiceManger.parseImageRequestBody(RequestKeys.PHOTO, file),
                                RetorfitServiceManger.parseRequestBody("icon")))
                .subscribe(object : FilterSubscriber<BaseResponseBean<*>>(context) {
                    override fun success(bean: BaseResponseBean<*>) {
                        view!!.loadSuccess()
                    }

                    override fun fail() {
                        super.fail()
                        view!!.loadFail("")
                    }
                }))


    }
}
